package com.dtsz.m2.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class DataUtils {

    public static void setMyPayToken(Context context, String token) {
       boolean flag =  getMyPayToken(context);
       if(!flag){
           Uri uri = Uri.parse("content://com.dtsz.m2.provider/record");
           ContentValues values = new ContentValues();
           values.put("name", "my_pay");
           values.put("token", token);
           Uri newUri = context.getContentResolver().insert(uri, values);
           Log.d("MainActivity", "book author is 哈哈哈");
       }
    }

    public static boolean getMyPayToken(Context context) {
        Uri uri = Uri.parse("content://com.dtsz.m2.provider/record");
        Cursor cursor = context.getContentResolver().query(uri, null, "name = 'my_pay'", null, null);
        boolean flag = false;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String author = cursor.getString(cursor.getColumnIndex("token"));
                Log.d("MainActivity", "book name is " + name);
                Log.d("MainActivity", "book author is " + author);
                flag = true;
            }
            cursor.close();
        }
        return flag;
    }
    public static String getMyPayTokenLogin(Context context) {
        Uri uri = Uri.parse("content://com.dtsz.m2.provider/record");
        Cursor cursor = context.getContentResolver().query(uri, null, "name = 'my_pay'", null, null);
        String author = null;
        if (cursor != null) {
            while (cursor.moveToNext()) {
                author = cursor.getString(cursor.getColumnIndex("token"));
                Log.d("MainActivity", "book author is " + author);
            }
            cursor.close();
        }
        return author;
    }

    public static void deleteToken(Context context) {
        Uri uri = Uri.parse("content://com.dtsz.m2.provider/record");
        context.getContentResolver().delete(uri, "name = 'my_pay'", null);
    }

}
